#include "chooseonepoker.h"
#include "ui_chooseonepoker.h"

ChooseOnePoker::ChooseOnePoker(QWidget *parent,QVector<int> need_choose_poker_v,int step_v) :
    QDialog(parent),
    ui(new Ui::ChooseOnePoker),
    need_choose_poker(),
    step(step_v),
    result(need_choose_poker_v[0])
{
    ui->setupUi(this);
    //根据数组设置
    for(int i = 0;i < need_choose_poker_v.size();++i){
        Poker * poker = new Poker(this,need_choose_poker_v[i]);
        need_choose_poker.push_back(poker);
        //连接点击事件
        connect(poker,&Poker::pos_changed,this,&ChooseOnePoker::clicked_one_poker);
    }
    //隐藏按钮
    ui->confirm->hide();
    //排列各个扑克牌
    sort_poker();
    //计时
    cd = new CountDown(this);
    cd->setPos(QPoint(180,30));
    cd->setTime(5);
    connect(cd,&CountDown::time_finish,this,&ChooseOnePoker::close);
    cd->start_time();
}

void ChooseOnePoker::closeEvent(QCloseEvent *e){
    e->accept();
}
int ChooseOnePoker::getResult() const{
    return this->result;
}

void ChooseOnePoker::sort_poker(){
    //设置第一张的位置
    int start_pos_x = need_choose_poker.size() * (need_choose_poker[0]->width() + step) - step;
    start_pos_x = (this->width() - start_pos_x)/2;
    int start_pos_y = (this->height() - need_choose_poker[0]->height()) / 2;
    int steps = step + need_choose_poker[0]->width();
    //排列
    for(int i = 0;i < need_choose_poker.size();++i){
        need_choose_poker[i]->move(start_pos_x + i * steps,start_pos_y);
        need_choose_poker[i]->show();
    }
}

//点击牌的事件
void ChooseOnePoker::clicked_one_poker(bool isUp,Poker * poker){
    //如果是选择
    if(isUp){

        result = poker->getIndex();//设置选择结果
        ui->confirm->show();//显示按钮
        //除了点击的那个，
        for(int i = 0;i < need_choose_poker.size();++i){
            //不是当前对象的，如果被点击了，就点击回去
            if(poker != need_choose_poker[i]){
                if(need_choose_poker[i]->getIsUp())
                    need_choose_poker[i]->clicked_my();
            }
        }
    //如果是取消选择
    }else{
        ui->confirm->hide();//显示按钮
    }

}

ChooseOnePoker::~ChooseOnePoker()
{
    delete ui;
}

void ChooseOnePoker::on_confirm_clicked()
{
    this->close();
}
